/*
 * StartApplication.java
 *
 * Created on Jun 24, 2010 3:07:33 PM;
 */

package org.sunspotworld;

import com.sun.spot.core.resources.transducers.ISwitch;
import com.sun.spot.core.resources.transducers.ITriColorLED;
import com.sun.spot.core.resources.transducers.LEDColor;
import com.sun.spot.edemo.EDemoBoard;

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
 * The startApp method of this class is called by the VM to start the
 * application.
 * 
 * The manifest specifies this class as MIDlet-1, which means it will
 * be selected for execution.
 */
public class StartApplication extends MIDlet {

    private ITriColorLED [] leds = EDemoBoard.getInstance().getLEDs();
    private ISwitch [] sw = EDemoBoard.getInstance().getSwitches();

    private int state = 0;
    private int red = 1;
    private int green = 2;
    private int blue = 3;
    private int clear = 4;

    private boolean CHANGESTATE = true;

    protected void startApp() throws MIDletStateChangeException {

        while(sw[0].isOpen() || sw[1].isOpen()) {
            if((sw[0].isClosed() || sw[1].isClosed()) && CHANGESTATE) {
                for(int i = 0; i < leds.length; i++) {
                    leds[i].setOff();
                }
                switch(state) {
                    case 1:
                        for(int i = 0; i < leds.length; i++) {
                            leds[i].setColor(LEDColor.RED);
                            leds[i].setOn();
                        }
                        break;
                    case 2:
                        for(int i = 0; i < leds.length; i++) {
                            leds[i].setColor(LEDColor.GREEN);
                            leds[i].setOn();
                        }
                        break;
                    case 3:
                        for(int i = 0; i < leds.length; i++) {
                            leds[i].setColor(LEDColor.BLUE);
                            leds[i].setOn();
                        }
                        break;
                    case 4:
                        for(int i = 0; i < leds.length; i++) {
                            leds[i].setColor(LEDColor.WHITE);
                            leds[i].setOn();
                        }
                        state = 0;
                        break;
                }
                state++;
                CHANGESTATE = false;
            }
            else if(sw[0].isOpen() && sw[1].isOpen()) {
                CHANGESTATE = true;
            }
        }


        notifyDestroyed();                      // cause the MIDlet to exit
    }

    protected void pauseApp() {
        // This is not currently called by the Squawk VM
    }

    /**
     * Called if the MIDlet is terminated by the system.
     * I.e. if startApp throws any exception other than MIDletStateChangeException,
     * if the isolate running the MIDlet is killed with Isolate.exit(), or
     * if VM.stopVM() is called.
     * 
     * It is not called if MIDlet.notifyDestroyed() was called.
     *
     * @param unconditional If true when this method is called, the MIDlet must
     *    cleanup and release all resources. If false the MIDlet may throw
     *    MIDletStateChangeException  to indicate it does not want to be destroyed
     *    at this time.
     */
    protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
        for (int i = 0; i < 8; i++) {
            leds[i].setOff();
        }
    }
}
